<?php
class Editpassword extends CI_Controller{
	
	private $user_info;

	public function __construct()
	{
		parent::__construct();
		//登录校验
		$this->common->check_login();
		//通过user_model的get_user_info_id方法获取个人信息
		$this->user_info = $this->user->get_user_info_id($_SESSION['user']['id']);
	}

	/**
	 * 修改密码页面显示
	 */
	public function index()
	{

		$this->load->helper('form');

		$data['left'] = $this->load->view('left', '', TRUE);

		$data['menu'] = $this->load->view('menu', '', TRUE);

		$this->load->view('editpwd', $data, FALSE);

	}

	/**
	 * 新密码提交
	 */
	public function info_post()
	{
		if(empty($_POST))
		{
			exit($this->common->alert_msg_goback('参数错误'));
		}
		
		
		//一级密码修改请求
		if($_POST['action'] == 'one')
		{
			$old_pwd = md5($_POST['old_pwd']);
			if($old_pwd != $this->user_info['pwd'])
			{
				exit($this->common->alert_msg_goback('原密码输入错误'));		
			}
			else
			{
				$result = $this->user->update_pwd($this->user_info['id'],md5($_POST['pwd']),1);

				if($result)
				{
					exit($this->common->alert_msg_url('密码更新成功',site_url('login')));
				}
				else
				{
					exit($this->common->alert_msg_goback('更新失败'));
				}
			}
		}
		//二级密码修改请求
		if($_POST['action'] == 'two'){
			$old_spwd = md5($_POST['old_spwd']);
			if($old_spwd != $this->user_info['spwd'])
			{
				exit($this->common->alert_msg_goback('原密码输入错误'));		
			}
			else
			{
				$result = $this->user->update_pwd($this->user_info['id'],md5($_POST['spwd']),2);

				if($result)
				{
					exit($this->common->alert_msg_url('密码更新成功',site_url('editpassword')));
				}
				else
				{
					exit($this->common->alert_msg_goback('更新失败'));
				}
			}
		}
	}
}